library(cjoint); library(gridExtra); library(dplyr); library(car)

load("appendix_data.RData")

# Define variables to pass into model
data$politicized <- as.factor(data$politicized)
data$treat_age <- as.factor(data$treat_age)
data$treat_gender <- as.factor(data$treat_gender)
data$treat_race <- as.factor(data$treat_race)
data$treat_lawS <- as.factor(data$treat_lawS)
data$treat_currentP <- as.factor(data$treat_currentP)
data$treat_pAbortion <- as.factor(data$treat_pAbortion)

######################################################################################################
# Figure A.1: Effect of Shared Descriptive Characteristics on Nominee Evaluations (5-Point Measure) #
#####################################################################################################
# Subset dataset to respondents who answered the support question
data.support <- data[!(is.na(data$support) | data$support==""), ]

# Estimate the AMCEs, cluster SEs by respondent
results.support.overall.5pt <- amce(support ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender +
                                  same.race, data=data.support,cluster=TRUE, respondent.id="caseid", weights="weight")
summary(results.support.overall.5pt)

# Subset dataset to respondents who answered the qualified question
data.qualified <- data[!(is.na(data$qualified) | data$qualified==""), ]

# Estimate the AMCEs, cluster SEs by respondent
results.qualified.overall.5pt <- amce(qualified ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender +
                                    same.race, data=data.qualified,cluster=TRUE, respondent.id="caseid",weights="weight")
summary(results.qualified.overall.5pt)

# Subset dataset to respondents who answered the trust question
data.trust <- data[!(is.na(data$trust) | data$trust==""), ]

# Estimate the AMCEs, cluster SEs by respondent
results.trust.overall.5pt <- amce(trust ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender +
                                same.race, data=data.trust,cluster=TRUE, respondent.id="caseid", weights="weight")
summary(results.trust.overall.5pt)

# Coefficients and SEs for support
# Make a coefficient plot
coef_gender_support <-0.0298364
coef_race_support <-0.2071583

se_gender_support <-0.031615
se_race_support <- 0.043515

# Create the confidence intervals
z_95 <- qnorm(.975)
ci_1 <- c(coef_gender_support - z_95*se_gender_support, coef_gender_support + z_95*se_gender_support)
ci_2 <- c(coef_race_support - z_95*se_race_support, coef_race_support + z_95*se_race_support)

gender_support <- data.frame(est = coef_gender_support, lb = ci_1[1], ub = ci_1[2], model = "Support")
race_support <- data.frame(est = coef_race_support, lb = ci_2[1], ub = ci_2[2], model = "Support")

# Coefficients and SEs for qualified
coef_gender_qualified <- 0.0090940
coef_race_qualified <- 0.1615862

se_gender_qualified <- 0.031837
se_race_qualified <- 0.048358

# Create the confidence intervals
z_95 <- qnorm(.975)
ci_1 <- c(coef_gender_qualified - z_95*se_gender_qualified, coef_gender_qualified + z_95*se_gender_qualified)
ci_2 <- c(coef_race_qualified - z_95*se_race_qualified, coef_race_qualified + z_95*se_race_qualified)

gender_qualified <- data.frame(est = coef_gender_qualified, lb = ci_1[1], ub = ci_1[2], model = "Qualified")
race_qualified <- data.frame(est = coef_race_qualified, lb = ci_2[1], ub = ci_2[2], model = "Qualified")

# Coefficients and SEs for trust
coef_gender_trust <-0.0565959
coef_race_trust <- 0.1959024

se_gender_trust <-0.029180
se_race_trust <- 0.042717

# Create the confidence intervals
z_95 <- qnorm(.975)
ci_1 <- c(coef_gender_trust - z_95*se_gender_trust, coef_gender_trust + z_95*se_gender_trust)
ci_2 <- c(coef_race_trust - z_95*se_race_trust, coef_race_trust + z_95*se_race_trust)

gender_trust <- data.frame(est = coef_gender_trust, lb = ci_1[1], ub = ci_1[2], model = "Trust")
race_trust <- data.frame(est = coef_race_trust, lb = ci_2[1], ub = ci_2[2], model = "Trust")

# Make the plot
dat <- as.data.frame(c("Qualified","Trust","Support"))
colnames(dat) <- c("order")

# Race
p1 <- ggplot(race_trust, aes(x = model, y = est)) +
  geom_point() +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2) +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Coefficient Estimate") +
  geom_point(data= race_qualified, aes(x = model, y = est)) +
  geom_errorbar(data=race_qualified, aes(ymin = lb, ymax = ub), width = 0.2) +
  geom_point(data= race_support, aes(x = model, y = est)) +
  geom_errorbar(data=race_support, aes(ymin = lb, ymax = ub), width = 0.2) +
  theme_bw() + coord_flip() + labs(title="Race")  + ylim(-.2,0.4) + scale_x_discrete(limits=dat$order) +
  theme(plot.title = element_text(hjust = 0.5))

# Gender
p2 <-ggplot(gender_trust, aes(x = model, y = est)) +
  geom_point() +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2) +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Coefficient Estimate") +
  geom_point(data= gender_qualified, aes(x = model, y = est)) +
  geom_errorbar(data=gender_qualified, aes(ymin = lb, ymax = ub), width = 0.2) +
  geom_point(data= gender_support, aes(x = model, y = est)) +
  geom_errorbar(data=gender_support, aes(ymin = lb, ymax = ub), width = 0.2) +
  theme_bw() + coord_flip() + labs(title="Gender")  + ylim(-.2,0.4) + scale_x_discrete(limits=dat$order) +
  theme(plot.title = element_text(hjust = 0.5))

grid.arrange(p1, p2, ncol=2)

###############################################################################################
# Figure A.2: Judicial Knowledge, Shared Descriptive Characteristics and Nominee Evaluations #
###############################################################################################

# Create combined knowledge measure
data.support$know1 <- recode(data.support$Q10,"'Appointed to the bench'=1;
                             c('Elected to the bench','Neither','NA')=0")
data.support$know1 <- recode(data.support$know1,"NA=0")
data.support$know2 <- recode(data.support$Q11,"'Life term'=1;
                             c('Set number of years','Neither','NA')=0")
data.support$know2 <- recode(data.support$know2,"NA=0")
data.support$know3 <- recode(data.support$Q12,"'U.S. Supreme Court'=1;
                             c('U.S. Congress','The President','NA')=0")
data.support$know3 <- recode(data.support$know3,"NA=0")
data.support$know4 <- recode(data.support$Q13,"'John Roberts'=1;
                             c('William Rehnquist','Stephen Breyer',
                             'Antonin Scalia','Anthony Kennedy','NA')=0")
data.support$know4 <- recode(data.support$know4,"NA=0")
data.support$know5 <- recode(data.support$Q14,'"Elena Kagan"=1;
                             c("Samuel Alito","Sonia Sotomayor",
                             "Sandra Day O\'Connor","John Roberts","NA")=0')
data.support$know5 <- recode(data.support$know5,"NA=0")

data.support$judicial.knowledge <- as.numeric(as.character(data.support$know1)) + as.numeric(as.character(data.support$know2)) + as.numeric(as.character(data.support$know3)) + as.numeric(as.character(data.support$know4)) + as.numeric(as.character(data.support$know5))
data.support$judicial.knowledge.high <- as.factor(recode(data.support$judicial.knowledge,"c(0,1,2,3)=0;c(4,5)=1") )

# Estimate the AMCEs, cluster SEs by respondent
results.support.knowledge.gender <- amce(support2 ~ politicized + treat_age + treat_gender + treat_race + same.gender*judicial.knowledge.high + same.race + treat_lawS + treat_currentP + treat_pAbortion,
                                         data=data.support, cluster=TRUE,
                                         respondent.id="caseid", weights="weight", respondent.varying="judicial.knowledge.high")
summary(results.support.knowledge.gender)


results.support.knowledge.race <- amce(support2 ~ politicized + treat_age + treat_gender + treat_race + same.gender + same.race*judicial.knowledge.high + treat_lawS + treat_currentP + treat_pAbortion,
                                       data=data.support, cluster=TRUE,
                                       respondent.id="caseid", weights="weight", respondent.varying="judicial.knowledge.high")
summary(results.support.knowledge.race)

# Coefficients and SEs for support and knowledge interaction
se_gender_low <- 0.015738
se_race_low <- 0.019324
se_gender_high <-  0.019478
se_race_high <- 0.027919

coef_gender_low <- 0.021789
coef_race_low <- 0.055914
coef_gender_high <- -0.0069956
coef_race_high <-0.075219


# Create the confidence intervals
z_95 <- qnorm(.975)
ci_1 <- c(coef_gender_low - z_95*se_gender_low, coef_gender_low + z_95*se_gender_low)
ci_2 <- c(coef_race_low - z_95*se_race_low, coef_race_low + z_95*se_race_low)
ci_3 <- c(coef_gender_high - z_95*se_gender_high, coef_gender_high + z_95*se_gender_high)
ci_4 <- c(coef_race_high - z_95*se_race_high, coef_race_high + z_95*se_race_high)

gender_low <- data.frame(est = coef_gender_low, lb = ci_1[1], ub = ci_1[2], model = "Gender (Low Knowledge)")
race_low <- data.frame(est = coef_race_low, lb = ci_2[1], ub = ci_2[2], model = "Race (Low Knowledge)")
gender_high <- data.frame(est = coef_gender_high, lb = ci_3[1], ub = ci_3[2], model = "Gender (High Knowledge)")
race_high <- data.frame(est = coef_race_high, lb = ci_4[1], ub = ci_4[2], model = "Race (High Knowledge)")

# Make the plot
ggplot(gender_low, aes(x = model, y = est)) +
  geom_point(colour="black") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Coefficient Estimate") +
  geom_point(data= race_low, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=race_low, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= race_high, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=race_high, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= gender_high, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=gender_high, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  + theme_bw() + coord_flip()


###############################################################################################
# Figure A.3: Partisanship, Race and Trust in Nominee Impartiality (Binary Measure) #
###############################################################################################

# Re Level the data to make sure that we are using respondent's race as the baseline
data.trust <- within(data.trust, race_nominee <- relevel(race_nominee, ref = "White"))
data.trust <- within(data.trust, gender_nominee <- relevel(gender_nominee, ref = "Male"))


# Trust, white respondents
data.trust.white <- data.trust[which(data.trust$race_respondent == "White"),]

# Democrats only
data.trust.white.dems <- data.trust.white[which(data.trust.white$pid3a == "Democrat"),]
results.trust.white.dems <- amce(trust2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                 data=data.trust.white.dems,cluster=TRUE,
                                 respondent.id="caseid",weights="weight")

# Republicans only
data.trust.white.reps <- data.trust.white[which(data.trust.white$pid3a == "Republican"),]
results.trust.white.reps <- amce(trust2 ~ gender_nominee + race_nominee + politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                 data=data.trust.white.reps,cluster=TRUE,
                                 respondent.id="caseid",weights="weight")

# Trust, Black respondents

# Re Level the data to make sure that we are using respondent's race as the baseline
data.trust <- within(data.trust, race_nominee <- relevel(race_nominee, ref = "Black"))
data.trust.black <- data.trust[which(data.trust$race_respondent == "Black"),]

# Democrats only
data.trust.black.dems <- data.trust.black[which(data.trust.black$pid3a == "Democrat"),]
results.trust.black.dems <- amce(trust2 ~ gender_nominee + race_nominee +  politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                 data=data.trust.black.dems,cluster=TRUE,
                                 respondent.id="caseid",weights="weight")

# Republicans only
data.trust.black.reps <- data.trust.black[which(data.trust.black$pid3a == "Republican"),]
results.trust.black.reps <- amce(trust2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                 data=data.trust.black.reps,cluster=TRUE,
                                 respondent.id="caseid",weights="weight")

# Trust, Hispanic respondents

# Re Level the data to make sure that we are using respondent's race as the baseline
data.trust <- within(data.trust, race_nominee <- relevel(race_nominee, ref = "Hispanic"))
yougov.hispanic <- data[which(data$race_respondent == "Hispanic"),]

data.trust.hispanic <- data.trust[which(data.trust$race_respondent == "Hispanic"),]

# Democrats only
data.trust.hispanic.dems <- data.trust.hispanic[which(data.trust.hispanic$pid3a == "Democrat"),]
results.trust.hispanic.dems <- amce(trust2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                    data=data.trust.hispanic.dems,cluster=TRUE,
                                    respondent.id="caseid",weights="weight")

# Republicans only
data.trust.hispanic.reps <- data.trust.hispanic[which(data.trust.hispanic$pid3a == "Republican"),]
results.trust.hispanic.reps <- amce(trust2 ~ gender_nominee + race_nominee+ politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                    data=data.trust.hispanic.reps,cluster=TRUE,
                                    respondent.id="caseid",weights="weight")

# Coefficients and SEs for Democrats
se.hispanicresp.dem.white <- 0.089825
se.hispanicresp.dem.black <- 0.078777

se.blackresp.dem.hispanic <- 0.058512
se.blackresp.dem.white <- 0.060110

se.whiteresp.dem.hispanic <- 0.024663
se.whiteresp.dem.black <-  0.031842

coef.hispanicresp.dem.white <- 0.069204
coef.hispanicresp.dem.black <- -0.043731

coef.blackresp.dem.hispanic <- -0.1364005
coef.blackresp.dem.white <- -0.2345870

coef.whiteresp.dem.hispanic <- 0.0380279
coef.whiteresp.dem.black <- 0.0770066


# Create the confidence intervals
z_95 <- qnorm(.975)
ci_1 <- c(coef.hispanicresp.dem.white - z_95*se.hispanicresp.dem.white, coef.hispanicresp.dem.white + z_95*se.hispanicresp.dem.white)
ci_2 <- c(coef.hispanicresp.dem.black - z_95*se.hispanicresp.dem.black, coef.hispanicresp.dem.black + z_95*se.hispanicresp.dem.black)
ci_3 <- c(coef.blackresp.dem.hispanic - z_95*se.blackresp.dem.hispanic, coef.blackresp.dem.hispanic + z_95*se.blackresp.dem.hispanic)
ci_4 <- c(coef.blackresp.dem.white - z_95*se.blackresp.dem.white, coef.blackresp.dem.white + z_95*se.blackresp.dem.white)
ci_5 <- c(coef.whiteresp.dem.hispanic - z_95*se.whiteresp.dem.hispanic, coef.whiteresp.dem.hispanic + z_95*se.whiteresp.dem.hispanic)
ci_6 <- c(coef.whiteresp.dem.black - z_95*se.whiteresp.dem.black, coef.whiteresp.dem.black + z_95*se.whiteresp.dem.black)

hispanicresp.dem.white <- data.frame(est = coef.hispanicresp.dem.white, lb = ci_1[1], ub = ci_1[2], model = "Hispanic-White")
hispanicresp.dem.black <- data.frame(est = coef.hispanicresp.dem.black, lb = ci_2[1], ub = ci_2[2], model = "Hispanic-Black")
blackresp.dem.hispanic <- data.frame(est = coef.blackresp.dem.hispanic, lb = ci_3[1], ub = ci_3[2], model = "Black-Hispanic")
blackresp.dem.white <- data.frame(est = coef.blackresp.dem.white, lb = ci_4[1], ub = ci_4[2], model = "Black-White")
whiteresp.dem.hispanic <- data.frame(est = coef.whiteresp.dem.hispanic, lb = ci_5[1], ub = ci_5[2], model = "White-Hispanic")
whiteresp.dem.black <- data.frame(est = coef.whiteresp.dem.black, lb = ci_6[1], ub = ci_6[2],  model = "White-Black")


# Coefficients and SEs for Republicans
se.hispanicresp.rep.white <- 0.119063
se.hispanicresp.rep.black <- 0.110455

se.blackresp.rep.hispanic <- 0.16202
se.blackresp.rep.white <- 0.17796

se.whiteresp.rep.hispanic <- 0.030534
se.whiteresp.rep.black <-  0.031648

coef.hispanicresp.rep.white <- -0.1642361
coef.hispanicresp.rep.black <- -0.0875098

coef.blackresp.rep.hispanic <- -0.312697
coef.blackresp.rep.white <- -0.094893

coef.whiteresp.rep.hispanic <- -0.016244
coef.whiteresp.rep.black <-  -0.053209


# Create the confidence intervals
ci_1 <- c(coef.hispanicresp.rep.white - z_95*se.hispanicresp.rep.white, coef.hispanicresp.rep.white + z_95*se.hispanicresp.rep.white)
ci_2 <- c(coef.hispanicresp.rep.black - z_95*se.hispanicresp.rep.black, coef.hispanicresp.rep.black + z_95*se.hispanicresp.rep.black)
ci_3 <- c(coef.blackresp.rep.hispanic - z_95*se.blackresp.rep.hispanic, coef.blackresp.rep.hispanic + z_95*se.blackresp.rep.hispanic)
ci_4 <- c(coef.blackresp.rep.white - z_95*se.blackresp.rep.white, coef.blackresp.rep.white + z_95*se.blackresp.rep.white)
ci_5 <- c(coef.whiteresp.rep.hispanic - z_95*se.whiteresp.rep.hispanic, coef.whiteresp.rep.hispanic + z_95*se.whiteresp.rep.hispanic)
ci_6 <- c(coef.whiteresp.rep.black - z_95*se.whiteresp.rep.black, coef.whiteresp.rep.black + z_95*se.whiteresp.rep.black)

hispanicresp.rep.white <- data.frame(est = coef.hispanicresp.rep.white, lb = ci_1[1], ub = ci_1[2], model = "Hispanic-White")
hispanicresp.rep.black <- data.frame(est = coef.hispanicresp.rep.black, lb = ci_2[1], ub = ci_2[2], model = "Hispanic-Black")
blackresp.rep.hispanic <- data.frame(est = coef.blackresp.rep.hispanic, lb = ci_3[1], ub = ci_3[2], model = "Black-Hispanic")
blackresp.rep.white <- data.frame(est = coef.blackresp.rep.white, lb = ci_4[1], ub = ci_4[2], model = "Black-White")
whiteresp.rep.hispanic <- data.frame(est = coef.whiteresp.rep.hispanic, lb = ci_5[1], ub = ci_5[2], model = "White-Hispanic")
whiteresp.rep.black <- data.frame(est = coef.whiteresp.rep.black, lb = ci_6[1], ub = ci_6[2],  model = "White-Black")

# Democrats
g1 <- ggplot(hispanicresp.dem.white, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") +
  geom_point(data= hispanicresp.dem.black, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=hispanicresp.dem.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_point(data= blackresp.dem.hispanic, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.dem.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70") +
  geom_point(data= blackresp.dem.white, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.dem.white, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70")  +
  geom_point(data= whiteresp.dem.hispanic, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.dem.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= whiteresp.dem.black, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.dem.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  +
  theme_bw() + coord_flip() + ylim(-.7,0.5) +
  labs(title="Democrats: Trust in Nominee Impartiality by Race") +
  theme(plot.title = element_text(hjust = 0.5))

# Republicans
g2 <- ggplot(hispanicresp.rep.white, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") +
  geom_point(data= hispanicresp.rep.black, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=hispanicresp.rep.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_point(data= blackresp.rep.hispanic, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.rep.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70") +
  geom_point(data= blackresp.rep.white, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.rep.white, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70")  +
  geom_point(data= whiteresp.rep.hispanic, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.rep.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= whiteresp.rep.black, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.rep.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  +
  theme_bw() + coord_flip()  + ylim(-.7,0.5) +
  labs(title="Republicans: Trust in Nominee Impartiality by Race") +
  theme(plot.title = element_text(hjust = 0.5))

grid.arrange(g1, g2, ncol=1, left= textGrob("Respondent-Candidate Pairing", rot=90), bottom=textGrob("Coefficient Estimate"))


###############################################################################
# Figure A.4: Partisanship, Race and Nominee Qualifications (Binary Measure) #
##############################################################################

# Re Level the data to make sure that we are using respondent's race as the baseline
data.qualified <- within(data.qualified, race_nominee <- relevel(race_nominee, ref = "White"))
data.qualified <- within(data.qualified, gender_nominee <- relevel(gender_nominee, ref = "Male"))


# Qualified, white respondents
data.qualified.white <- data.qualified[which(data.qualified$race_respondent == "White"),]

# Democrats only
data.qualified.white.dems <- data.qualified.white[which(data.qualified.white$pid3a == "Democrat"),]
results.qualified.white.dems <- amce(qualified2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                     data=data.qualified.white.dems,cluster=TRUE,
                                     respondent.id="caseid",weights="weight")

# Republicans only
data.qualified.white.reps <- data.qualified.white[which(data.qualified.white$pid3a == "Republican"),]
results.qualified.white.reps <- amce(qualified2 ~ gender_nominee + race_nominee + politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                     data=data.qualified.white.reps,cluster=TRUE,
                                     respondent.id="caseid",weights="weight")

# Qualified, Black respondents

# Re Level the data to make sure that we are using respondent's race as the baseline
data.qualified <- within(data.qualified, race_nominee <- relevel(race_nominee, ref = "Black"))
data.qualified.black <- data.qualified[which(data.qualified$race_respondent == "Black"),]


# Democrats only
data.qualified.black.dems <- data.qualified.black[which(data.qualified.black$pid3a == "Democrat"),]
results.qualified.black.dems <- amce(qualified2 ~ gender_nominee + race_nominee +  politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                     data=data.qualified.black.dems,cluster=TRUE,
                                     respondent.id="caseid",weights="weight")

# Republicans only
data.qualified.black.reps <- data.qualified.black[which(data.qualified.black$pid3a == "Republican"),]
results.qualified.black.reps <- amce(qualified2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                     data=data.qualified.black.reps,cluster=TRUE,
                                     respondent.id="caseid",weights="weight")

# Qualified, Hispanic respondents

# Re Level the data to make sure that we are using respondent's race as the baseline
data.qualified <- within(data.qualified, race_nominee <- relevel(race_nominee, ref = "Hispanic"))
data.qualified.hispanic <- data.qualified[which(data.qualified$race_respondent == "Hispanic"),]

# Democrats only
data.qualified.hispanic.dems <- data.qualified.hispanic[which(data.qualified.hispanic$pid3a == "Democrat"),]
results.qualified.hispanic.dems <- amce(qualified2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                        data=data.qualified.hispanic.dems,cluster=TRUE,
                                        respondent.id="caseid",weights="weight")

# Republicans only
data.qualified.hispanic.reps <- data.qualified.hispanic[which(data.qualified.hispanic$pid3a == "Republican"),]
results.qualified.hispanic.reps <- amce(qualified2 ~ gender_nominee + race_nominee+ politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                        data=data.qualified.hispanic.reps,cluster=TRUE,
                                        respondent.id="caseid",weights="weight")

# Coefficients and SEs for Democrats
se.hispanicresp.dem.white <- 0.065766
se.hispanicresp.dem.black <- 0.077770

se.blackresp.dem.hispanic <- 0.072084
se.blackresp.dem.white <- 0.050053

se.whiteresp.dem.hispanic <- 0.031023
se.whiteresp.dem.black <-  0.036913

coef.hispanicresp.dem.white <- -0.0687146
coef.hispanicresp.dem.black <- -0.0066937

coef.blackresp.dem.hispanic <- -0.07991862
coef.blackresp.dem.white <- -0.15624990

coef.whiteresp.dem.hispanic <- 0.0249701
coef.whiteresp.dem.black <- 0.0496341


# Create the confidence intervals
ci_1 <- c(coef.hispanicresp.dem.white - z_95*se.hispanicresp.dem.white, coef.hispanicresp.dem.white + z_95*se.hispanicresp.dem.white)
ci_2 <- c(coef.hispanicresp.dem.black - z_95*se.hispanicresp.dem.black, coef.hispanicresp.dem.black + z_95*se.hispanicresp.dem.black)
ci_3 <- c(coef.blackresp.dem.hispanic - z_95*se.blackresp.dem.hispanic, coef.blackresp.dem.hispanic + z_95*se.blackresp.dem.hispanic)
ci_4 <- c(coef.blackresp.dem.white - z_95*se.blackresp.dem.white, coef.blackresp.dem.white + z_95*se.blackresp.dem.white)
ci_5 <- c(coef.whiteresp.dem.hispanic - z_95*se.whiteresp.dem.hispanic, coef.whiteresp.dem.hispanic + z_95*se.whiteresp.dem.hispanic)
ci_6 <- c(coef.whiteresp.dem.black - z_95*se.whiteresp.dem.black, coef.whiteresp.dem.black + z_95*se.whiteresp.dem.black)

hispanicresp.dem.white <- data.frame(est = coef.hispanicresp.dem.white, lb = ci_1[1], ub = ci_1[2], model = "Hispanic-White")
hispanicresp.dem.black <- data.frame(est = coef.hispanicresp.dem.black, lb = ci_2[1], ub = ci_2[2], model = "Hispanic-Black")
blackresp.dem.hispanic <- data.frame(est = coef.blackresp.dem.hispanic, lb = ci_3[1], ub = ci_3[2], model = "Black-Hispanic")
blackresp.dem.white <- data.frame(est = coef.blackresp.dem.white, lb = ci_4[1], ub = ci_4[2], model = "Black-White")
whiteresp.dem.hispanic <- data.frame(est = coef.whiteresp.dem.hispanic, lb = ci_5[1], ub = ci_5[2], model = "White-Hispanic")
whiteresp.dem.black <- data.frame(est = coef.whiteresp.dem.black, lb = ci_6[1], ub = ci_6[2], model = "White-Black")


# Coefficients and SEs for Republicans
se.hispanicresp.rep.white <- 0.114892
se.hispanicresp.rep.black <- 0.111226

se.blackresp.rep.hispanic <- 0.137968
se.blackresp.rep.white <- 0.122147

se.whiteresp.rep.hispanic <- 0.033191
se.whiteresp.rep.black <-  0.031223

coef.hispanicresp.rep.white <- -0.2200228
coef.hispanicresp.rep.black <- -0.0017158

coef.blackresp.rep.hispanic <- -0.527848
coef.blackresp.rep.white <- -0.346457

coef.whiteresp.rep.hispanic <- -0.0187308
coef.whiteresp.rep.black <- -0.0768528


# Create the confidence intervals
ci_1 <- c(coef.hispanicresp.rep.white - z_95*se.hispanicresp.rep.white, coef.hispanicresp.rep.white + z_95*se.hispanicresp.rep.white)
ci_2 <- c(coef.hispanicresp.rep.black - z_95*se.hispanicresp.rep.black, coef.hispanicresp.rep.black + z_95*se.hispanicresp.rep.black)
ci_3 <- c(coef.blackresp.rep.hispanic - z_95*se.blackresp.rep.hispanic, coef.blackresp.rep.hispanic + z_95*se.blackresp.rep.hispanic)
ci_4 <- c(coef.blackresp.rep.white - z_95*se.blackresp.rep.white, coef.blackresp.rep.white + z_95*se.blackresp.rep.white)
ci_5 <- c(coef.whiteresp.rep.hispanic - z_95*se.whiteresp.rep.hispanic, coef.whiteresp.rep.hispanic + z_95*se.whiteresp.rep.hispanic)
ci_6 <- c(coef.whiteresp.rep.black - z_95*se.whiteresp.rep.black, coef.whiteresp.rep.black + z_95*se.whiteresp.rep.black)

hispanicresp.rep.white <- data.frame(est = coef.hispanicresp.rep.white, lb = ci_1[1], ub = ci_1[2], model = "Hispanic-White")
hispanicresp.rep.black <- data.frame(est = coef.hispanicresp.rep.black, lb = ci_2[1], ub = ci_2[2], model = "Hispanic-Black")
blackresp.rep.hispanic <- data.frame(est = coef.blackresp.rep.hispanic, lb = ci_3[1], ub = ci_3[2], model = "Black-Hispanic")
blackresp.rep.white <- data.frame(est = coef.blackresp.rep.white, lb = ci_4[1], ub = ci_4[2],  model = "Black-White")
whiteresp.rep.hispanic <- data.frame(est = coef.whiteresp.rep.hispanic,lb = ci_5[1], ub = ci_5[2], model = "White-Hispanic")
whiteresp.rep.black <- data.frame(est = coef.whiteresp.rep.black, lb = ci_6[1], ub = ci_6[2], model = "White-Black")

# Make the plot

# Democrats
g1 <- ggplot(hispanicresp.dem.white, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") +
  geom_point(data= hispanicresp.dem.black, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=hispanicresp.dem.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_point(data= blackresp.dem.hispanic, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.dem.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70") +
  geom_point(data= blackresp.dem.white, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.dem.white, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70")  +
  geom_point(data= whiteresp.dem.hispanic, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.dem.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= whiteresp.dem.black, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.dem.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  +
  theme_bw() + coord_flip() + ylim(-.9,0.5) +
  labs(title="Democrats: Nominee Qualifications by Race") +
  theme(plot.title = element_text(hjust = 0.5))

# Republicans
g2 <- ggplot(hispanicresp.rep.white, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") +
  geom_point(data= hispanicresp.rep.black, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=hispanicresp.rep.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_point(data= blackresp.rep.hispanic, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.rep.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70") +
  geom_point(data= blackresp.rep.white, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.rep.white, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70")  +
  geom_point(data= whiteresp.rep.hispanic, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.rep.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= whiteresp.rep.black, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.rep.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  +
  theme_bw() + coord_flip()  + ylim(-.9,0.5) +
  labs(title="Republicans: Nominee Qualifications by Race") +
  theme(plot.title = element_text(hjust = 0.5))

grid.arrange(g1, g2, ncol=1, left= textGrob("Respondent-Candidate Pairing", rot=90), bottom=textGrob("Coefficient Estimate"))

########################################################
# Figure A.5: Partisanship, Race, and Nominee Support #
#######################################################

# Support, white respondents

# Re Level the data to make sure that we are using respondent's race as the baseline
data.support <- within(data.support, race_nominee <- relevel(race_nominee, ref = "White"))
data.support.white <- data.support[which(data.support$race_respondent == "White"),]

# Democrats only
data.support.white.dems <- data.support.white[which(data.support.white$pid3a == "Democrat"),]
results.support.white.dems <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                   data=data.support.white.dems,cluster=TRUE,
                                   respondent.id="caseid",weights="weight")

# Republicans and Independents only
data.support.white.reps <- data.support.white[which(data.support.white$pid3a == "Republican" | data.support.white$pid3a=="Independent"),]
results.support.white.reps <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                   data=data.support.white.reps,cluster=TRUE,
                                   respondent.id="caseid",weights="weight")

# Support, Black respondents

# Re Level the data to make sure that we are using respondent's race as the baseline
data.support <- within(data.support, race_nominee <- relevel(race_nominee, ref = "Black"))
data.support.black <- data.support[which(data.support$race_respondent == "Black"),]

# Democrats only
data.support.black.dems <- data.support.black[which(data.support.black$pid3a == "Democrat"),]
results.support.black.dems <- amce(support2 ~ gender_nominee + race_nominee +  politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                   data=data.support.black.dems,cluster=TRUE,
                                   respondent.id="caseid",weights="weight")

# Republicans and Independents only
data.support.black.reps <- data.support.black[which(data.support.black$pid3a == "Republican" | data.support.black$pid3a=="Independent"),]
results.support.black.reps <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                   data=data.support.black.reps,cluster=TRUE,
                                   respondent.id="caseid",weights="weight")

# Support, Hispanic respondents

# Re Level the data to make sure that we are using respondent's race as the baseline
data.support <- within(data.support, race_nominee <- relevel(race_nominee, ref = "Hispanic"))
data.support.hispanic <- data.support[which(data.support$race_respondent == "Hispanic"),]

# Democrats only
data.support.hispanic.dems <- data.support.hispanic[which(data.support.hispanic$pid3a == "Democrat"),]
results.support.hispanic.dems <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                      data=data.support.hispanic.dems,cluster=TRUE,
                                      respondent.id="caseid",weights="weight")

# Republicans and Independents only
data.support.hispanic.reps <- data.support.hispanic[which(data.support.hispanic$pid3a == "Republican" | data.support.hispanic$pid3a=="Independent"),]
results.support.hispanic.reps <- amce(support2 ~ gender_nominee + race_nominee+ politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.gender,
                                      data=data.support.hispanic.reps,cluster=TRUE,
                                      respondent.id="caseid",weights="weight")

# Coefficients and SEs for Democrats
se.hispanicresp.dem.white <- 0.084162
se.hispanicresp.dem.black <- 0.078847

se.blackresp.dem.hispanic <- 0.056423
se.blackresp.dem.white <- 0.054109

se.whiteresp.dem.hispanic <- 0.027374
se.whiteresp.dem.black <-  0.033561

coef.hispanicresp.dem.white <- -0.035620
coef.hispanicresp.dem.black <- 0.062250

coef.blackresp.dem.hispanic <- -0.1676413
coef.blackresp.dem.white <- -0.2253979

coef.whiteresp.dem.hispanic <- 0.0240293
coef.whiteresp.dem.black <- 0.0452267


# Create the confidence intervals
ci_1 <- c(coef.hispanicresp.dem.white - z_95*se.hispanicresp.dem.white, coef.hispanicresp.dem.white + z_95*se.hispanicresp.dem.white)
ci_2 <- c(coef.hispanicresp.dem.black - z_95*se.hispanicresp.dem.black, coef.hispanicresp.dem.black + z_95*se.hispanicresp.dem.black)
ci_3 <- c(coef.blackresp.dem.hispanic - z_95*se.blackresp.dem.hispanic, coef.blackresp.dem.hispanic + z_95*se.blackresp.dem.hispanic)
ci_4 <- c(coef.blackresp.dem.white - z_95*se.blackresp.dem.white, coef.blackresp.dem.white + z_95*se.blackresp.dem.white)
ci_5 <- c(coef.whiteresp.dem.hispanic - z_95*se.whiteresp.dem.hispanic, coef.whiteresp.dem.hispanic + z_95*se.whiteresp.dem.hispanic)
ci_6 <- c(coef.whiteresp.dem.black - z_95*se.whiteresp.dem.black, coef.whiteresp.dem.black + z_95*se.whiteresp.dem.black)

#graph the coefficients and their confidence intervals
hispanicresp.dem.white <- data.frame(est = coef.hispanicresp.dem.white, lb = ci_1[1], ub = ci_1[2], model = "Hispanic-White")
hispanicresp.dem.black <- data.frame(est = coef.hispanicresp.dem.black, lb = ci_2[1], ub = ci_2[2], model = "Hispanic-Black")
blackresp.dem.hispanic <- data.frame(est = coef.blackresp.dem.hispanic, lb = ci_3[1], ub = ci_3[2], model = "Black-Hispanic")
blackresp.dem.white <- data.frame(est = coef.blackresp.dem.white, lb = ci_4[1], ub = ci_4[2], model = "Black-White")
whiteresp.dem.hispanic <- data.frame(est = coef.whiteresp.dem.hispanic, lb = ci_5[1], ub = ci_5[2], model = "White-Hispanic")
whiteresp.dem.black <- data.frame(est = coef.whiteresp.dem.black, lb = ci_6[1], ub = ci_6[2], model = "White-Black")


# Coefficients and SEs for Republicans
se.hispanicresp.rep.white <- 0.049327
se.hispanicresp.rep.black <- 0.063217

se.blackresp.rep.hispanic <- 0.064957
se.blackresp.rep.white <- 0.073964

se.whiteresp.rep.hispanic <- 0.021805
se.whiteresp.rep.black <-  0.020299

coef.hispanicresp.rep.white <- -0.0252824
coef.hispanicresp.rep.black <-  0.1061138

coef.blackresp.rep.hispanic <- -0.0973707
coef.blackresp.rep.white <- -0.1136076

coef.whiteresp.rep.hispanic <- -0.0278392
coef.whiteresp.rep.black <-  -0.0095213


# Create the confidence intervals
ci_1 <- c(coef.hispanicresp.rep.white - z_95*se.hispanicresp.rep.white, coef.hispanicresp.rep.white + z_95*se.hispanicresp.rep.white)
ci_2 <- c(coef.hispanicresp.rep.black - z_95*se.hispanicresp.rep.black, coef.hispanicresp.rep.black + z_95*se.hispanicresp.rep.black)
ci_3 <- c(coef.blackresp.rep.hispanic - z_95*se.blackresp.rep.hispanic, coef.blackresp.rep.hispanic + z_95*se.blackresp.rep.hispanic)
ci_4 <- c(coef.blackresp.rep.white - z_95*se.blackresp.rep.white, coef.blackresp.rep.white + z_95*se.blackresp.rep.white)
ci_5 <- c(coef.whiteresp.rep.hispanic - z_95*se.whiteresp.rep.hispanic, coef.whiteresp.rep.hispanic + z_95*se.whiteresp.rep.hispanic)
ci_6 <- c(coef.whiteresp.rep.black - z_95*se.whiteresp.rep.black, coef.whiteresp.rep.black + z_95*se.whiteresp.rep.black)

hispanicresp.rep.white <- data.frame(est = coef.hispanicresp.rep.white, lb = ci_1[1], ub = ci_1[2], model = "Hispanic-White")
hispanicresp.rep.black <- data.frame(est = coef.hispanicresp.rep.black, lb = ci_2[1], ub = ci_2[2], model = "Hispanic-Black")
blackresp.rep.hispanic <- data.frame(est = coef.blackresp.rep.hispanic, lb = ci_3[1], ub = ci_3[2], model = "Black-Hispanic")
blackresp.rep.white <- data.frame(est = coef.blackresp.rep.white, lb = ci_4[1], ub = ci_4[2], model = "Black-White")
whiteresp.rep.hispanic <- data.frame(est = coef.whiteresp.rep.hispanic, lb = ci_5[1], ub = ci_5[2], model = "White-Hispanic")
whiteresp.rep.black <- data.frame(est = coef.whiteresp.rep.black, lb = ci_6[1], ub = ci_6[2], model = "White-Black")

# Make the plot

# Democrats
g1 <- ggplot(hispanicresp.dem.white, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") +
  geom_point(data= hispanicresp.dem.black, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=hispanicresp.dem.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_point(data= blackresp.dem.hispanic, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.dem.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70") +
  geom_point(data= blackresp.dem.white, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.dem.white, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70")  +
  geom_point(data= whiteresp.dem.hispanic, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.dem.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= whiteresp.dem.black, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.dem.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  +
  theme_bw() + coord_flip() + ylim(-.7,0.5) +
  labs(title="Democrats: Support for Nominees by Race") +
  theme(plot.title = element_text(hjust = 0.5))


# Republicans and Independents
g2 <- ggplot(hispanicresp.rep.white, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") +
  geom_point(data= hispanicresp.rep.black, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=hispanicresp.rep.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_point(data= blackresp.rep.hispanic, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.rep.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70") +
  geom_point(data= blackresp.rep.white, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.rep.white, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70")  +
  geom_point(data= whiteresp.rep.hispanic, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.rep.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= whiteresp.rep.black, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.rep.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  +
  theme_bw() + coord_flip()  + ylim(-.7,0.5) +
  labs(title="Republicans and Independents: Support for Nominees by Race") +
  theme(plot.title = element_text(hjust = 0.5))

grid.arrange(g1, g2, ncol=1, left= textGrob("Respondent-Candidate Pairing", rot=90), bottom=textGrob("Coefficient Estimate"))


#########################################################################
# Figure A.6: Confidence in Supreme Court and Number of Female Justices #
#########################################################################
load("longitudinal_data.RData")

plot(confidence_women ~ year, data=confidence, pch=16, type="l", ylim=c(0.2,0.5), col="darkgreen",
     ylab="Percent Confident in Supreme Court", xlab="Year", xaxt="n", lwd=2, lty=0)
axis(side=1, at=c(1975,1980,1985,1990,1995,2000,2005,2010,2015), labels=c(1975,1980,1985,1990,1995,2000,2005,2010,2015))
polygon(x=c(1980,1980,1992,1992,1980), y=c(0.2,0.5,0.5,0.2,0.2), col="grey90")
polygon(x=c(1992,1992,2004,2004,1992), y=c(0.2,0.5,0.5,0.2,0.2), col="grey70")
polygon(x=c(2004,2004,2007,2007,2004), y=c(0.2,0.5,0.5,0.2,0.2), col="grey90")
polygon(x=c(2007,2007,2011,2011,2007), y=c(0.2,0.5,0.5,0.2,0.2), col="grey70")
polygon(x=c(2011,2011,2015,2015,2011), y=c(0.2,0.5,0.5,0.2,0.2), col="grey50")

lines(x=confidence$year, y=confidence$confidence_women, col="black", lwd=2, lty=2)
lines(x=confidence$year, y=confidence$confidence_men, col="darkblue", lwd=2)
text(x=1975, y=0.30, "Women",col="black", cex=1)
text(x=1975, y=0.42, "Men",col="darkblue", cex=1)

mtext(side=3, at=1980, text="O'Connor", cex=.75)
mtext(side=3, at=1992, text="Ginsburg", cex=.75)
mtext(side=3, at=2002, text="O'Connor Retires", cex=.75)
mtext(side=3, at=2007, text="Sotomayor", cex=.75)
mtext(side=3, at=2011, text="Kagan", cex=.75)

#########################################################################
# Table A.1: Interactive Relationship between Nominee Race and Gender #
#########################################################################

data.support$white_female <- as.factor(data.support$treat_gender == 2 & data.support$treat_race == 1)
data.support$black_female <-as.factor(data.support$treat_gender == 2 & data.support$treat_race == 2)
data.support$hispanic_female <- as.factor(data.support$treat_gender == 2 & data.support$treat_race == 3)
data.support$black_male <- as.factor(data.support$treat_gender == 1 & data.support$treat_race == 2)
data.support$hispanic_male <- as.factor(data.support$treat_gender == 1 & data.support$treat_race == 3)

# Overall Support
results.support.indicators <- amce(support2 ~ politicized+ treat_age + treat_lawS + treat_currentP + treat_pAbortion + white_female
                                   + black_female + hispanic_female + black_male + hispanic_male,
                                   data=data.support,cluster=TRUE,
                                   respondent.id="caseid",weights="weight")
summary(results.support.indicators)

# Republican support
data.support.rep <- data.support[data.support$pid3a == "Republican",]
results.support.indicators.republicans <- amce(support2 ~ politicized+ treat_age + treat_lawS + treat_currentP + treat_pAbortion + white_female
                                               + black_female + hispanic_female + black_male + hispanic_male,
                                               data=data.support.rep,cluster=TRUE,
                                               respondent.id="caseid",weights="weight")
summary(results.support.indicators.republicans)

# Democrat support
data.support.dem <- data.support[data.support$pid3a == "Democrat",]
results.support.indicators.democrats <- amce(support2 ~ politicized+ treat_age + treat_lawS + treat_currentP + treat_pAbortion + white_female
                                             + black_female + hispanic_female + black_male + hispanic_male,
                                             data=data.support.dem,cluster=TRUE,
                                             respondent.id="caseid",weights="weight")
summary(results.support.indicators.democrats)

# White Male Republican support
data.support.white.male.rep <- data.support[data.support$pid3a == "Republican" &
                                              data.support$race_respondent == "White" &
                                              data.support$gender_respondent == "Male",]
results.support.indicators.white.male.republicans <- amce(support2 ~ politicized+ treat_age + treat_lawS + treat_currentP + treat_pAbortion + white_female
                                                          + black_female + hispanic_female + black_male + hispanic_male,
                                                          data=data.support.white.male.rep,cluster=TRUE,
                                                          respondent.id="caseid",weights="weight")
summary(results.support.indicators.white.male.republicans)

# White Male Democrat support
data.support.white.male.dem <- data.support[data.support$pid3a == "Democrat" &
                                              data.support$race_respondent == "White" &
                                              data.support$gender_respondent == "Male",]
results.support.indicators.white.male.democrats <- amce(support2 ~ politicized+ treat_age + treat_lawS + treat_currentP + treat_pAbortion + white_female
                                                        + black_female + hispanic_female + black_male + hispanic_male,
                                                        data=data.support.white.male.dem,cluster=TRUE,
                                                        respondent.id="caseid",weights="weight")
summary(results.support.indicators.white.male.democrats)

# White Female Democrat support
data.support.white.female.dem <- data.support[data.support$pid3a == "Democrat" &
                                                data.support$race_respondent == "White" &
                                                data.support$gender_respondent == "Female",]
results.support.indicators.white.female.democrats <- amce(support2 ~ politicized+ treat_age + treat_lawS + treat_currentP + treat_pAbortion + white_female
                                                          + black_female + hispanic_female + black_male + hispanic_male,
                                                          data=data.support.white.female.dem,cluster=TRUE,
                                                          respondent.id="caseid",weights="weight")
summary(results.support.indicators.white.female.democrats)

# White Female Republican support
data.support.white.female.rep <- data.support[data.support$pid3a == "Republican" &
                                                data.support$race_respondent == "White" &
                                                data.support$gender_respondent == "Female",]
results.support.indicators.white.female.republicans <- amce(support2 ~ politicized+ treat_age + treat_lawS + treat_currentP + treat_pAbortion + white_female
                                                            + black_female + hispanic_female + black_male + hispanic_male,
                                                            data=data.support.white.female.rep,cluster=TRUE,
                                                            respondent.id="caseid",weights="weight")
summary(results.support.indicators.white.female.republicans)


######################################
# Table B.1: Descriptive Statistics #
#####################################

round(prop.table(table(data$gender_respondent)), 3)
round(prop.table(table(data$race_respondent)), 3)
round(prop.table(table(data$educ)), 3)
round(prop.table(table(data$faminc2)), 3)
round(prop.table(table(data$pid3)), 3)
round(prop.table(table(data$ideo5)), 3)


########################################################################################################
# Table B.3: Effect of Descriptive Representation on Attitudes toward Judicial Nominees: Full Results #
#######################################################################################################

# Estimate the AMCEs for support (binary), cluster SEs by respondent
results.support.overall.binary <- amce(support2 ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender + 
                                  same.race, data=data.support,cluster=TRUE, respondent.id="caseid", weights="weight")
summary(results.support.overall.binary)

# Estimate the AMCEs trust (binary), cluster SEs by respondent
results.trust.overall.binary <- amce(trust2 ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender + 
                                same.race, data=data.trust,cluster=TRUE, respondent.id="caseid", weights="weight")
summary(results.trust.overall.binary)

# Estimate the AMCEs qualified (binary), cluster SEs by respondent
results.qualified.overall.binary <- amce(qualified2 ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender + 
                                    same.race, data=data.qualified,cluster=TRUE, respondent.id="caseid",weights="weight")
summary(results.qualified.overall.binary)

####################################################################################################################
# Figure C.1: Effect of Shared Descriptive Characteristics on Nominee Evaluations (Bootstrapped Standard Errors) #
###################################################################################################################

# Set the seed to run the bootstrap and create empty lists to store results
set.seed(02138)
nboot <- 1000

supportresults  <- list()
qualifiedresults  <- list()
trustresults  <- list()

# Unique respondent IDs
caseids.support <- unique(data.support$caseid)
caseids.qualified <- unique(data.qualified$caseid)
caseids.trust <- unique(data.trust$caseid)

for (k in 1:nboot) {
  
  # Sample respondents to be used in this bootstrap iteration
  respondent_sample_support   <- sample(caseids.support,  length(caseids.support),replace=TRUE)
  respondent_sample_qualified <- sample(caseids.qualified,length(caseids.qualified),replace=TRUE)
  respondent_sample_trust     <- sample(caseids.trust,    length(caseids.trust),replace=TRUE)
  
  # Sample all of the profiles that respondent received 
  support_dataset <-   inner_join(tibble(caseid = respondent_sample_support), data.support, by = "caseid")
  qualified_dataset <- inner_join(tibble(caseid = respondent_sample_qualified), data.qualified, by = "caseid")
  trust_dataset <-     inner_join(tibble(caseid = respondent_sample_trust), data.trust, by = "caseid")
  
  # Then, run regressions
  support_bootdat_nocov <- amce(support2 ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender +
                                  same.race,
                                data=support_dataset,cluster=TRUE,
                                respondent.id="caseid",weights="weight")
  
  qualified_bootdat_nocov <- amce(qualified2 ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender +
                                    same.race,
                                  data=qualified_dataset,cluster=TRUE,
                                  respondent.id="caseid",weights="weight")
  
  trust_bootdat_nocov <- amce(trust2 ~ politicized + treat_age + treat_gender + treat_race +  treat_lawS + treat_currentP + treat_pAbortion + same.gender +
                                same.race,
                              data=trust_dataset,cluster=TRUE,
                              respondent.id="caseid",weights="weight")
  
  
  
  supportresults[[k]]  <-lapply(support_bootdat_nocov$estimates, function(i) i[1])[c("samerace", "samegender")]
  qualifiedresults[[k]]  <-lapply(qualified_bootdat_nocov$estimates, function(i) i[1])[c("samerace", "samegender")]
  trustresults[[k]]  <-lapply(trust_bootdat_nocov$estimates, function(i) i[1])[c("samerace", "samegender")]
  
  if (k %% 250 == 0) cat(paste(k, "out of", nboot, "samples stored..\n"))
  
}

# Turn the bootstrap results into our CI estimates
# SEs
support_vec_se    <- apply(do.call(rbind.data.frame,supportresults), 2, sd)
qualified_vec_se  <- apply(do.call(rbind.data.frame,qualifiedresults), 2, sd)
trust_vec_se      <- apply(do.call(rbind.data.frame,trustresults), 2, sd)

# point estimates
support_vec_est    <- apply(do.call(rbind.data.frame,supportresults), 2, mean)
qualified_vec_est  <- apply(do.call(rbind.data.frame,qualifiedresults), 2, mean)
trust_vec_est     <- apply(do.call(rbind.data.frame,trustresults), 2, mean)


# Data frames for plotting
gender_support <- data.frame(est = support_vec_est["samegender"],
                             lb =  support_vec_est["samegender"] -  qnorm(.975)*support_vec_se["samegender"],
                             ub =  support_vec_est["samegender"] +  qnorm(.975)*support_vec_se["samegender"],
                             model = "Support")


race_support <-  data.frame(est = support_vec_est["samerace"],
                            lb =  support_vec_est["samerace"] -  qnorm(.975)*support_vec_se["samerace"],
                            ub =  support_vec_est["samerace"] +  qnorm(.975)*support_vec_se["samerace"],
                            model = "Support")

gender_qualified <- data.frame(est = qualified_vec_est["samegender"],
                               lb  = qualified_vec_est["samegender"] -  qnorm(.975)*qualified_vec_se["samegender"],
                               ub  = qualified_vec_est["samegender"] +  qnorm(.975)*qualified_vec_se["samegender"],
                               model = "Qualified")


race_qualified <-  data.frame(est = qualified_vec_est["samerace"],
                              lb  = qualified_vec_est["samerace"] -  qnorm(.975)*qualified_vec_se["samerace"],
                              ub  = qualified_vec_est["samerace"] +  qnorm(.975)*qualified_vec_se["samerace"],
                              model = "Qualified")


gender_trust <- data.frame(est = trust_vec_est["samegender"],
                           lb  = trust_vec_est["samegender"] -  qnorm(.975)*trust_vec_se["samegender"],
                           ub  = trust_vec_est["samegender"] +  qnorm(.975)*trust_vec_se["samegender"],
                           model = "Trust")


race_trust <-  data.frame(est = trust_vec_est["samerace"],
                          lb  = trust_vec_est["samerace"] -  qnorm(.975)*trust_vec_se["samerace"],
                          ub  = trust_vec_est["samerace"] +  qnorm(.975)*trust_vec_se["samerace"],
                          model = "Trust")

dat <- as.data.frame(c("Qualified","Trust","Support"))
colnames(dat) <- c("order")

# Gender
p1 <-ggplot(gender_trust, aes(x = model, y = est)) +
  geom_point() +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2) +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Coefficient Estimate") +
  geom_point(data= gender_qualified, aes(x = model, y = est)) +
  geom_errorbar(data=gender_qualified, aes(ymin = lb, ymax = ub), width = 0.2) +
  geom_point(data= gender_support, aes(x = model, y = est)) +
  geom_errorbar(data=gender_support, aes(ymin = lb, ymax = ub), width = 0.2) +
  theme_bw() + coord_flip() + labs(title="Gender")  + ylim(-.1,0.2) + scale_x_discrete(limits=dat$order) +
  theme(plot.title = element_text(hjust = 0.5))


# Race
p2 <- ggplot(race_trust, aes(x = model, y = est)) +
  geom_point() +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2) +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Coefficient Estimate") +
  geom_point(data= race_qualified, aes(x = model, y = est)) +
  geom_errorbar(data=race_qualified, aes(ymin = lb, ymax = ub), width = 0.2) +
  geom_point(data= race_support, aes(x = model, y = est)) +
  geom_errorbar(data=race_support, aes(ymin = lb, ymax = ub), width = 0.2) +
  theme_bw() + coord_flip() + labs(title="Race")  + ylim(-.1,0.2) + scale_x_discrete(limits=dat$order) +
  theme(plot.title = element_text(hjust = 0.5))

grid.arrange(p2, p1, ncol=2, nrow=1)



#######################################################################################
# Figure C.2: Partisanship, Race, and Nominee Support (Bootstrapped Standard Errors) #
######################################################################################
# Create empty lists to store bootstrap results
supportresults.whitedems  <-list()
supportresults.whitereps  <-list()
supportresults.blackdems  <-list()
supportresults.blackreps  <-list()
supportresults.hispanicdems  <-list()
supportresults.hispanicreps <- list()
supportresults.womendems <- list()
supportresults.womenreps <- list()
supportresults.mendems <- list()
supportresults.menreps <- list()
ran.black.rep.regression <- rep(NA, nboot)

for(k in 1:nboot){
  
  # Sample respondents to be used in this bootstrap iteration
  
  respondent_sample_support   <- sample(caseids.support,  length(caseids.support),replace=TRUE)
  support_dataset <-   inner_join(tibble(caseid = respondent_sample_support), data.support, by = "caseid")
  
  # Then, run regressions #

  # Re-level the data to make sure that we are using respondent's race as the baseline 
  support_dataset <- within(support_dataset, race_nominee <- relevel(race_nominee, ref = "White"))
  support_dataset <- within(support_dataset, gender_nominee <- relevel(gender_nominee, ref = "Male"))
  
  
  # Support, white respondents
  support_dataset.white <- support_dataset[which(support_dataset$race_respondent == "White"),]
  
  # Democrats only
  support_dataset.white.dems <- support_dataset.white[which(support_dataset.white$pid3a == "Democrat"),]
  results.support.white.dems <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender, 
                                     data=support_dataset.white.dems,cluster=TRUE,
                                     respondent.id="caseid",weights="weight")
  
  # Republicans only
  support_dataset.white.reps <- support_dataset.white[which(support_dataset.white$pid3a == "Republican"),]
  results.support.white.reps <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.gender, 
                                     data=support_dataset.white.reps,cluster=TRUE,
                                     respondent.id="caseid",weights="weight")
  
  # Support, Black respondents
  
  # Re-level the data to make sure that we are using respondent's race as the baseline 
  support_dataset <- within(support_dataset, race_nominee <- relevel(race_nominee, ref = "Black"))
  support_dataset.black <- support_dataset[which(support_dataset$race_respondent == "Black"),]
  
  
  # Democrats only
  support_dataset.black.dems <- support_dataset.black[which(support_dataset.black$pid3a == "Democrat"),]
  results.support.black.dems <- amce(support2 ~ gender_nominee + race_nominee +  politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender, 
                                     data=support_dataset.black.dems,cluster=TRUE,
                                     respondent.id="caseid",weights="weight")
  
  # Republicans only
  support_dataset.black.reps <- support_dataset.black[which(support_dataset.black$pid3a == "Republican"),]
  
  # Ensuring there is enough variation in the data to run the regression. This may occur due to low number of black Reps in sample and respondnets receiving the same value of politicized across all four profiles
  if (length(unique(support_dataset.black.reps$politicized)) > 1){
    results.support.black.reps <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender, 
                                       data=support_dataset.black.reps,cluster=TRUE,
                                       respondent.id="caseid",weights="weight")
    ran.black.rep.regression[k] <- 1
   } 

  # Support, Hispanic respondents
  
  # Re-level the data to make sure that we are using respondent's race as the baseline 
  support_dataset <- within(support_dataset, race_nominee <- relevel(race_nominee, ref = "Hispanic"))
  
  support_dataset.hispanic <- support_dataset[which(support_dataset$race_respondent == "Hispanic"),]
  
  # Democrats only
  support_dataset.hispanic.dems <- support_dataset.hispanic[which(support_dataset.hispanic$pid3a == "Democrat"),]
  results.support.hispanic.dems <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.gender, 
                                        data=support_dataset.hispanic.dems,cluster=TRUE,
                                        respondent.id="caseid",weights="weight")
  
  # Republicans only
  support_dataset.hispanic.reps <- support_dataset.hispanic[which(support_dataset.hispanic$pid3a == "Republican"),]
  results.support.hispanic.reps <- amce(support2 ~ gender_nominee + race_nominee+ politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.gender, 
                                        data=support_dataset.hispanic.reps,cluster=TRUE,
                                        respondent.id="caseid",weights="weight")
  
  
  # Now looking at support by gender match
  support_dataset.male <- support_dataset[which(support_dataset$gender_respondent == "Male"),]
  
  # Democrats only
  support_dataset.male.dems <- support_dataset.male[which(support_dataset.male$pid3a == "Democrat"),]
  results.support.male.dems <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.race, 
                                    data=support_dataset.male.dems,cluster=TRUE,
                                    respondent.id="caseid",weights="weight")
  
  # Republicans only
  support_dataset.male.reps <- support_dataset.male[which(support_dataset.male$pid3a == "Republican"),]
  results.support.male.reps <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age +  treat_lawS + treat_currentP + treat_pAbortion + same.race, 
                                    data=support_dataset.male.reps,cluster=TRUE,
                                    respondent.id="caseid",weights="weight")
  
  # Re-level the data to make sure that we are using respondent's gender as the baseline 
  support_dataset <- within(support_dataset, gender_nominee <- relevel(gender_nominee, ref = "Female"))
  support_dataset.female <- support_dataset[which(support_dataset$gender_respondent == "Female"),]
  
  # Democrats only
  support_dataset.female.dems <- support_dataset.female[which(support_dataset.female$pid3a == "Democrat"),]
  results.support.female.dems <- amce(support2 ~ gender_nominee + race_nominee +  politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.race, 
                                      data=support_dataset.female.dems,cluster=TRUE,
                                      respondent.id="caseid",weights="weight")
  
  # Republicans only
  support_dataset.female.reps <- support_dataset.female[which(support_dataset.female$pid3a == "Republican"),]
  results.support.female.reps <- amce(support2 ~ gender_nominee + race_nominee + politicized + treat_age + treat_lawS + treat_currentP + treat_pAbortion + same.race, 
                                      data=support_dataset.female.reps,cluster=TRUE,
                                      respondent.id="caseid",weights="weight")

  supportresults.whitedems[[k]]  <-lapply(results.support.white.dems$estimates, function(i) i[1,])[c("racenominee")] 
  supportresults.whitereps[[k]]  <-lapply(results.support.white.reps$estimates, function(i) i[1,])[c("racenominee")] 
  supportresults.blackdems[[k]]  <-lapply(results.support.black.dems$estimates, function(i) i[1,])[c("racenominee")] 
  supportresults.blackreps[[k]]  <-lapply(results.support.black.reps$estimates, function(i) i[1,])[c("racenominee")] 
  supportresults.hispanicdems[[k]]  <-lapply(results.support.hispanic.dems$estimates, function(i) i[1,])[c("racenominee")] 
  supportresults.hispanicreps[[k]]  <-lapply(results.support.hispanic.reps$estimates, function(i) i[1,])[c("racenominee")] 
  supportresults.womendems[[k]]  <-lapply(results.support.female.dems$estimates, function(i) i[1])[c("gendernominee")] 
  supportresults.womenreps[[k]]  <-lapply(results.support.female.reps$estimates, function(i) i[1])[c("gendernominee")] 
  supportresults.mendems[[k]]  <-lapply(results.support.male.dems$estimates, function(i) i[1])[c("gendernominee")] 
  supportresults.menreps[[k]]  <-lapply(results.support.male.reps$estimates, function(i) i[1])[c("gendernominee")] 
  
  if (k %% 250 == 0) cat(paste(k, "out of", nboot, "samples stored..\n"))
  
}

# Removing iterations for which no black Republican regression could be run
supportresults.blackreps2 <- supportresults.blackreps[!is.na(ran.black.rep.regression)]

# Turn the bootstrap results into our CI estimates  
# SEs
supportresults.whitedems_vec_se  <- apply(t(rbind.data.frame(supportresults.whitedems)), 2, sd, na.rm=TRUE)
supportresults.whitereps_vec_se  <- apply(t(rbind.data.frame(supportresults.whitereps)), 2, sd, na.rm=TRUE)
supportresults.blackdems_vec_se  <- apply(t(rbind.data.frame(supportresults.blackdems)), 2, sd, na.rm=TRUE)
supportresults.blackreps_vec_se  <- apply(t(rbind.data.frame(supportresults.blackreps2)), 2, sd, na.rm=TRUE)
supportresults.hispanicdems_vec_se   <- apply(t(rbind.data.frame(supportresults.hispanicdems)), 2, sd, na.rm=TRUE)
supportresults.hispanicreps_vec_se   <- apply(t(rbind.data.frame(supportresults.hispanicreps)), 2, sd, na.rm=TRUE)
supportresults.womendems_vec_se   <- apply(do.call(rbind.data.frame,supportresults.womendems), 2, sd, na.rm=TRUE)
supportresults.womenreps_vec_se   <-  apply(do.call(rbind.data.frame,supportresults.womenreps), 2, sd, na.rm=TRUE)
supportresults.mendems_vec_se   <-  apply(do.call(rbind.data.frame,supportresults.mendems), 2, sd, na.rm=TRUE)
supportresults.menreps_vec_se   <-  apply(do.call(rbind.data.frame,supportresults.menreps), 2, sd, na.rm=TRUE)

# Point estimates
supportresults.whitedems_vec_est  <- apply(t(rbind.data.frame(supportresults.whitedems)), 2, mean, na.rm=TRUE)
supportresults.whitereps_vec_est  <- apply(t(rbind.data.frame(supportresults.whitereps)), 2, mean, na.rm=TRUE)
supportresults.blackdems_vec_est  <- apply(t(rbind.data.frame(supportresults.blackdems)), 2, mean, na.rm=TRUE)
supportresults.blackreps_vec_est  <- apply(t(rbind.data.frame(supportresults.blackreps2)), 2, mean, na.rm=TRUE)
supportresults.hispanicdems_vec_est   <- apply(t(rbind.data.frame(supportresults.hispanicdems)), 2, mean, na.rm=TRUE)
supportresults.hispanicreps_vec_est   <- apply(t(rbind.data.frame(supportresults.hispanicreps)), 2, mean, na.rm=TRUE)
supportresults.womendems_vec_est   <- apply(do.call(rbind.data.frame,supportresults.womendems), 2, mean, na.rm=TRUE)
supportresults.womenreps_vec_est   <-  apply(do.call(rbind.data.frame,supportresults.womenreps), 2, mean, na.rm=TRUE)
supportresults.mendems_vec_est   <-  apply(do.call(rbind.data.frame,supportresults.mendems), 2, mean, na.rm=TRUE)
supportresults.menreps_vec_est   <-  apply(do.call(rbind.data.frame,supportresults.menreps), 2, mean, na.rm=TRUE)

# Data frames for plotting
whiteresp.dem.hispanic <-  data.frame(est = supportresults.whitedems_vec_est["racenomineeHispanic"],
                                   lb = supportresults.whitedems_vec_est["racenomineeHispanic"] -  qnorm(.975)*supportresults.whitedems_vec_se["racenomineeHispanic"],
                                   ub = supportresults.whitedems_vec_est["racenomineeHispanic"] +  qnorm(.975)*supportresults.whitedems_vec_se["racenomineeHispanic"],
                                   model = "White-Hispanic")

whiteresp.dem.black <-  data.frame(est = supportresults.whitedems_vec_est["racenomineeBlack"],
                                      lb = supportresults.whitedems_vec_est["racenomineeBlack"] -  qnorm(.975)*supportresults.whitedems_vec_se["racenomineeBlack"],
                                      ub = supportresults.whitedems_vec_est["racenomineeBlack"] +  qnorm(.975)*supportresults.whitedems_vec_se["racenomineeBlack"],
                                      model = "White-Black")


blackresp.dem.hispanic <- data.frame(est = supportresults.blackdems_vec_est["racenomineeHispanic"],
                                     lb = supportresults.blackdems_vec_est["racenomineeHispanic"] -  qnorm(.975)*supportresults.blackdems_vec_se["racenomineeHispanic"],
                                     ub = supportresults.blackdems_vec_est["racenomineeHispanic"] +  qnorm(.975)*supportresults.blackdems_vec_se["racenomineeHispanic"], 
                                     model = "Black-Hispanic")


blackresp.dem.white <- data.frame(est = supportresults.blackdems_vec_est["racenomineeWhite"],
                                  lb = supportresults.blackdems_vec_est["racenomineeWhite"] -  qnorm(.975)*supportresults.blackdems_vec_se["racenomineeWhite"],
                                  ub = supportresults.blackdems_vec_est["racenomineeWhite"] +  qnorm(.975)*supportresults.blackdems_vec_se["racenomineeWhite"], 
                                  model = "Black-White")


hispanicresp.dem.white <- data.frame(est = supportresults.hispanicdems_vec_est["racenomineeWhite"],
                                     lb =  supportresults.hispanicdems_vec_est["racenomineeWhite"] -  qnorm(.975)*supportresults.hispanicdems_vec_se["racenomineeWhite"],
                                     ub =  supportresults.hispanicdems_vec_est["racenomineeWhite"] +  qnorm(.975)*supportresults.hispanicdems_vec_se["racenomineeWhite"],
                                     model = "Hispanic-White")


hispanicresp.dem.black <- data.frame(est = supportresults.hispanicdems_vec_est["racenomineeBlack"],
                                     lb =  supportresults.hispanicdems_vec_est["racenomineeBlack"] - qnorm(.975)*supportresults.hispanicdems_vec_se["racenomineeBlack"],
                                     ub = supportresults.hispanicdems_vec_est["racenomineeBlack"] +  qnorm(.975)*supportresults.hispanicdems_vec_se["racenomineeBlack"],
                                     model = "Hispanic-Black")

whiteresp.rep.hispanic <-  data.frame(est = supportresults.whitereps_vec_est["racenomineeHispanic"],
                                      lb = supportresults.whitereps_vec_est["racenomineeHispanic"] -  qnorm(.975)*supportresults.whitereps_vec_se["racenomineeHispanic"],
                                      ub = supportresults.whitereps_vec_est["racenomineeHispanic"] +  qnorm(.975)*supportresults.whitereps_vec_se["racenomineeHispanic"],
                                      model = "White-Hispanic")

whiteresp.rep.black <-  data.frame(est = supportresults.whitereps_vec_est["racenomineeBlack"],
                                   lb = supportresults.whitereps_vec_est["racenomineeBlack"] -  qnorm(.975)*supportresults.whitereps_vec_se["racenomineeBlack"],
                                   ub = supportresults.whitereps_vec_est["racenomineeBlack"] +  qnorm(.975)*supportresults.whitereps_vec_se["racenomineeBlack"],
                                   model = "White-Black")


blackresp.rep.hispanic <- data.frame(est = supportresults.blackreps_vec_est["racenomineeHispanic"],
                                     lb = supportresults.blackreps_vec_est["racenomineeHispanic"] -  qnorm(.975)*supportresults.blackreps_vec_se["racenomineeHispanic"],
                                     ub = supportresults.blackreps_vec_est["racenomineeHispanic"] +  qnorm(.975)*supportresults.blackreps_vec_se["racenomineeHispanic"], 
                                     model = "Black-Hispanic")


blackresp.rep.white <- data.frame(est = supportresults.blackreps_vec_est["racenomineeWhite"],
                                  lb = supportresults.blackreps_vec_est["racenomineeWhite"] -  qnorm(.975)*supportresults.blackreps_vec_se["racenomineeWhite"],
                                  ub = supportresults.blackreps_vec_est["racenomineeWhite"] +  qnorm(.975)*supportresults.blackreps_vec_se["racenomineeWhite"], 
                                  model = "Black-White")


hispanicresp.rep.white <- data.frame(est = supportresults.hispanicreps_vec_est["racenomineeWhite"],
                                     lb =  supportresults.hispanicreps_vec_est["racenomineeWhite"] -  qnorm(.975)*supportresults.hispanicreps_vec_se["racenomineeWhite"],
                                     ub =  supportresults.hispanicreps_vec_est["racenomineeWhite"] +  qnorm(.975)*supportresults.hispanicreps_vec_se["racenomineeWhite"],
                                     model = "Hispanic-White")


hispanicresp.rep.black <- data.frame(est = supportresults.hispanicreps_vec_est["racenomineeBlack"],
                                     lb =  supportresults.hispanicreps_vec_est["racenomineeBlack"] - qnorm(.975)*supportresults.hispanicreps_vec_se["racenomineeBlack"],
                                     ub = supportresults.hispanicreps_vec_est["racenomineeBlack"] +  qnorm(.975)*supportresults.hispanicreps_vec_se["racenomineeBlack"],
                                     model = "Hispanic-Black")


g3 <- ggplot(hispanicresp.dem.white, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") + 
  geom_point(data= hispanicresp.dem.black, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=hispanicresp.dem.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_point(data= blackresp.dem.hispanic, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.dem.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70") +
  geom_point(data= blackresp.dem.white, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.dem.white, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70")  + 
  geom_point(data= whiteresp.dem.hispanic, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.dem.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= whiteresp.dem.black, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.dem.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  + 
  theme_bw() + coord_flip() + ylim(-1.75,1.5) + 
  labs(title="Democrats: Support for Nominees by Race") +
  theme(plot.title = element_text(hjust = 0.5))


g4 <- ggplot(hispanicresp.rep.white, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") + 
  geom_point(data= hispanicresp.rep.black, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=hispanicresp.rep.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_point(data= blackresp.rep.hispanic, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.rep.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70") +
  geom_point(data= blackresp.rep.white, aes(x = model, y = est),colour="grey70") +
  geom_errorbar(data=blackresp.rep.white, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey70")  + 
  geom_point(data= whiteresp.rep.hispanic, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.rep.hispanic, aes(ymin = lb, ymax = ub), width = 0.2, colour="black") +
  geom_point(data= whiteresp.rep.black, aes(x = model, y = est),colour="black") +
  geom_errorbar(data=whiteresp.rep.black, aes(ymin = lb, ymax = ub), width = 0.2, colour="black")  + 
  theme_bw() + coord_flip()  + 
  labs(title="Republicans: Support for Nominees by Race") +  
  theme(plot.title = element_text(hjust = 0.5)) + ylim(-1.75,1.5)

grid.arrange(g3, g4, ncol=1, left= textGrob("Respondent-Candidate Pairing", rot=90), bottom=textGrob("Coefficient Estimate"))

########################################################################################
# Figure C.3: Partisanship, Gender, and Nominee Support (Bootstrapped Standard Errors) #
########################################################################################

# Graph the coefficients and their confidence intervals
maleresp.dem.female <- data.frame(est = supportresults.mendems_vec_est["gendernominee"],
                                  lb =  supportresults.mendems_vec_est["gendernominee"] - qnorm(.975)*supportresults.mendems_vec_se["gendernominee"],
                                  ub = supportresults.mendems_vec_est["gendernominee"] + qnorm(.975)*supportresults.mendems_vec_se["gendernominee"],
                                  model = "Male-Female")


femaleresp.dem.male <- data.frame(est = supportresults.womendems_vec_est["gendernominee"],
                                  lb =  supportresults.womendems_vec_est["gendernominee"] - qnorm(.975)*supportresults.womendems_vec_se["gendernominee"],
                                  ub =  supportresults.womendems_vec_est["gendernominee"] + qnorm(.975)*supportresults.womendems_vec_se["gendernominee"],
                                  model = "Female-Male")

maleresp.rep.female <- data.frame(est = supportresults.menreps_vec_est["gendernominee"],
                                  lb =  supportresults.menreps_vec_est["gendernominee"] - qnorm(.975)*supportresults.menreps_vec_se["gendernominee"],
                                  ub = supportresults.menreps_vec_est["gendernominee"] + qnorm(.975)*supportresults.menreps_vec_se["gendernominee"],
                                  model = "Male-Female")


femaleresp.rep.male <- data.frame(est = supportresults.womenreps_vec_est["gendernominee"],
                                  lb = supportresults.womenreps_vec_est["gendernominee"] - qnorm(.975)*supportresults.womenreps_vec_se["gendernominee"],
                                  ub = supportresults.womenreps_vec_est["gendernominee"] + qnorm(.975)*supportresults.womenreps_vec_se["gendernominee"],
                                  model = "Female-Male")

g1 <- ggplot(maleresp.dem.female, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") +
  geom_point(data= femaleresp.dem.male, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=femaleresp.dem.male, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  theme_bw() + coord_flip() + ylim(-.25,0.25) +
  labs(title="Democrats: Support for Nominees by Gender") +
  theme(plot.title = element_text(hjust = 0.5))


g2 <- ggplot(maleresp.rep.female, aes(x = model, y = est)) +
  geom_point(colour="grey45") +
  geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("") +
  geom_point(data= femaleresp.rep.male, aes(x = model, y = est),colour="grey45") +
  geom_errorbar(data=femaleresp.rep.male, aes(ymin = lb, ymax = ub), width = 0.2, colour="grey45") +
  theme_bw() + coord_flip() + ylim(-.25,0.25) +
  labs(title="Republicans: Support for Nominees by Gender") +
  theme(plot.title = element_text(hjust = 0.5))

grid.arrange(g1, g2, ncol=1, left= textGrob("Respondent-Candidate Pairing", rot=90), bottom=textGrob("Coefficient Estimate"))
